package com.binance.notification.web.service.abnormaltrade.integration;

import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.binance.master.error.GeneralCode;
import com.binance.master.models.APIRequest;
import com.binance.master.models.APIResponse;
import com.binance.mbxgateway.api.IKlineApi;
import com.binance.mbxgateway.vo.request.klines.GetKlineRequest;
import com.binance.mbxgateway.vo.response.klines.GetKlinesResponse;
import com.binance.mbxgateway.vo.response.klines.GetKlinesResponse.KLineItem;
import com.binance.notification.web.service.abnormaltrade.enums.IntervalEnum;
import com.binance.notification.web.service.abnormaltrade.log.LoggerUtils;
import com.binance.notification.web.service.abnormaltrade.models.KlineInfo;
import com.google.common.collect.Lists;

/**
 * 对接撮合的k线接口
 */
@Service
public class KlineService {
    private static final org.slf4j.Logger log = LoggerUtils.getLogger(KlineService.class);
    @Resource
    private IKlineApi klineApi;

    /**
     * 获取K线数据
     *
     * @param symbol
     * @param limit
     * @param interval
     * @param startTime
     * @param endTime
     * @return
     */
    public List<KlineInfo> fetchKline(String symbol, Integer limit, IntervalEnum interval, Long startTime,
            Long endTime) {
        GetKlineRequest body = new GetKlineRequest();
        body.setSymbol(symbol);
        body.setLimit(Long.valueOf(limit));
        body.setInterval(interval.getName());
        body.setStartTime(startTime);
        body.setEndTime(endTime);
        APIResponse<GetKlinesResponse> response;
        try {
            response = this.klineApi.getKlines(APIRequest.instance(body));
        } catch (Exception e) {
            log.warn("从mbx获取kline出错：{}", e);
            return null;
        }
        if (GeneralCode.SUCCESS.getCode().equals(response.getCode())) {
            List<KLineItem> items = response.getData().getItems();
            List<KlineInfo> klineInfos = Lists.newArrayList();
            for (KLineItem item : items) {
                klineInfos.add(this.wrapKlineInfo(item));
            }
            return klineInfos;
        }else {
            log.warn("从mbx获取kline出错：{}", response.toString());
            return null;
        }
    }


    private KlineInfo wrapKlineInfo(KLineItem item) {
        MathContext mc = new MathContext(8, RoundingMode.HALF_UP);
        KlineInfo klineInfo = new KlineInfo();
        klineInfo.setOpenTime(item.getOpenTime());
        klineInfo.setOpen(item.getOpen().round(mc));
        klineInfo.setHigh(item.getHigh().round(mc));
        klineInfo.setLow(item.getLow().round(mc));
        klineInfo.setClose(item.getClose().round(mc));
        klineInfo.setVolume(item.getVolume().round(mc));
        klineInfo.setCloseTime(item.getCloseTime());
        klineInfo.setQuoteAssetVolume(item.getQuoteAssetVolume().round(mc));
        return klineInfo;
    }
}
